<?php

namespace App\GameChannel;

use App\Models\Orders;
use App\Services\ChannelService;
use App\Services\GameChannelService;
use Illuminate\Http\Request;

class SdkBase
{
    protected $gameId;
    protected $channel;
    protected $gameChannel;
    protected $loginNotify;
    protected $payNotify;

    function __construct($game_id, $channel, $game_channel)
    {
        $this->gameId      = $game_id;
        $this->channel     = $channel;
        $this->gameChannel = $game_channel;
        $this->loginNotify = new LoginNotify($game_id, $channel);
        $this->payNotify   = new PayNotify($game_id, $channel);
    }

    // 返回充值回调地址
    public function callbackUrl()
    {
        return config('app.url') . "/api/pay/callback/" . $this->gameChannel['id'];
    }

    // 渠道登录
    public function login(Request $request): LoginNotify
    {
        return $this->loginNotify;
    }

    // 创建订单-渠道信息整理返回
    public function payInit(Orders $order, Request $request)
    {
        return [];
    }

    /**
     * 处理充值回调
     * @param Request $request 请求参数
     * @param string  $cb_type 谁回调过来 server,client
     * @return PayNotify
     */
    public function payCb(Request $request, $cb_type): PayNotify
    {
        return $this->payNotify;
    }

    // 支付成功补齐订单其他信息
    public function supplementOrderInfo()
    {
        return true;
    }

    // 失败返回
    public function payErr()
    {
        return yqerr($this->payNotify->errno);
    }

    // 成功返回
    public function payOk()
    {
        return yqok();
    }

    /**
     * 能否使用切支付 1能 0否
     * @return int
     */
    public function canSwitchPay($package_id = 9999)
    {
        if ($this->gameChannel['switch_pay'] === 0) {
            return 0;
        }

        $unswitch_pay_list = json_decode($this->gameChannel['unswitch_pay_list'], true);
        if (in_array($package_id, $unswitch_pay_list)) {
            return 0;
        }

        return 1;
    }

    /**
     * 切支付地址，给与前端通过webview拉起
     * @return string
     */
    public function switchPayUrl(Orders $order)
    {
        return config('my.switch_pay_url') . "/switch_pay/index/" . $order->trade_no;
    }

    /**
     * 根据渠道获取处理对象
     * @param integer $game_id 游戏id
     * @param integer $channel 渠道id
     * @param integer $game_channel_id 渠道配置id
     * @return int|static
     */
    public static function loadSDK($game_id, $channel, $game_channel_id)
    {
        $channel_info = ChannelService::getInstance()->one($channel);
        if (empty($channel_info)) {
            return false;
        }

        //首字母大写
        $flag           = ucfirst($channel_info['channel_flag']);
        $sdk_class      = "{$flag}Sdk";
        $sdk_class_path = app_path("GameChannel/Sdks/{$sdk_class}.php");
        if (!file_exists($sdk_class_path)) {
            return false;
        }

        $game_channel = GameChannelService::getInstance()->get($game_channel_id);
        if (!$game_channel) {
            return false;
        }

        $class = "App\\GameChannel\\Sdks\\{$sdk_class}";
        return new $class($game_id, $channel, $game_channel);
    }
}
